﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using urBook.Engine;

namespace urBook.Gui
{
    /// <summary>
    /// Interaction logic for ContactItem.xaml
    /// </summary>
    public partial class ContactItem : UserControl
    {

        public ContactCard contact { get; set; }
        public MainWindow root { get; set; }
        public ContactsList parent { get; set; }

        public ContactItem(MainWindow root, ContactsList parent, ContactCard contact)
        {
            this.InitializeComponent();
            this.root = root;
            this.parent = parent;
            this.contact = contact;
            if (contact.photo.url != "")
                setImageSource(contactImage, contact.photo.url);
            contactName.Content = contact.name.formattedName;
            if (contact.phones.Count > 0)
                contactPhone.Content = contact.phones[0].value;
            else if (contact.emails.Count > 0)
                contactPhone.Content = contact.emails[0].value;

            ContextAppend.Click += root.Append_Click;
            ContextDelete.Click += root.Delete_Click;
            ContextEdit.Click += root.Edit_Click;
            ContextExport.Click += root.Export_Click;
            ContextMerge.Click += root.Merge_Click;
            
            foreach (Group g in root.addressBook.groups)
            {
                MenuItem item1 = new MenuItem();
                item1.Header = g.name;
                item1.Click += root.AddToGroup_Click;
                ContextAdd.Items.Add(item1);
                MenuItem item2 = new MenuItem();
                item2.Header = g.name;
                item2.Click += root.MoveToGroup_Click;
                ContextMove.Items.Add(item2);
            }
        }

        private void setImageSource(Image image, string source)
        {
            BitmapImage src = new BitmapImage();
            src.BeginInit();
            src.UriSource = new Uri(source, UriKind.RelativeOrAbsolute);
            src.CacheOption = BitmapCacheOption.OnLoad;
            src.EndInit();
            image.Source = src;
            image.Stretch = Stretch.Uniform;
        }

        void delete_MouseDown(object sender, MouseButtonEventArgs e)
        {
            String message = "Are you sure that you would like to delete this Contact?";
            String caption = "Delete " + contact.name.formattedName;
            MessageBoxResult result = MessageBox.Show(message, caption, MessageBoxButton.YesNo, MessageBoxImage.Question);

            if (result == MessageBoxResult.Yes)
            {
                root.addressBook.remove(contact);
                parent.list.Items.Remove(this);
            }
        }

        void edit_MouseDown(object sender, MouseButtonEventArgs e)
        {
            root.viewContact(this);
        }

        void ContactItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            root.viewContact(this);
        }
    }
}